<?php
    session_start();
    require_once('userlevel.php'); //Require/include the permissions file
    require_once('template.php'); //Require/include the template file
    require_once('db.php'); //Require/include the file with the database connection information
    require_once('functions.php'); //Require/include the functions file
     
    if (!$_SESSION['user_loggedin']) {
        die ("Not logged in... <script>document.location.href='login.php'</script>");
    } elseif (!($_SESSION['userlevel'] >= $userlevel['addfile'])) {
        die (errorpage ("Your user level is not high enough to use this feature!", 'Add file to be tracked'));
    } else {
        //Check to see if the form has been submitted by checking if any of the fields have data in them
        if (isset($_POST['filepath']) OR isset($_POST['filename']) OR isset($_POST['filenotes'])) {
            //Check if the required fields have been filled out
            if (empty($_POST['filepath'])) {
                die(errorpage ("Please enter a filepath!", 'Add file to be tracked'));
            }
            if (empty($_POST['filename'])) {
                die(errorpage ("Please enter a filename!", 'Add file to be tracked'));
            }
             
            template_headtag("Add New File");
            template_header();
            template_left();
             
            //Connect to DB using db.php
            $db = new my_db;
             
            //Clean up vars to make sure they are safe to insert into the DB
            $filepath = mysql_escape_string($_POST['filepath']);
            $filename = mysql_escape_string($_POST['filename']);
             
            $status = addfile ($filepath, $filename);
            if ($status == 'badfile') {
                echo "<div class='pageheadertext'>Add new file to be tracked</div>";
                echo "<br/>";                
                
                errorbox("File does not exist!");
                echo "<br/><div align='center'><a href='javascript:history.back(1)'>Back</a></div>";
            } elseif($status == 'already') {
                echo "<div class='pageheadertext'>Add file to be tracked</div>";
                echo "<br/>";
                errorbox("File is already being tracked!");
                echo "<br/><div align='center'><a href='javascript:history.back(1)'>Back</a></div>";
            } elseif($status == 'cannotread') {
                echo "<div class='pageheadertext'>Add file to be tracked</div>";
                echo "<br/>";
                errorbox("Can not read file, permission denied!");
                echo "<br/><div align='center'><a href='javascript:history.back(1)'>Back</a></div>";
            } elseif($status == 'isdir') {
                echo "<div class='pageheadertext'>Add file to be tracked</div>";
                echo "<br/>";
                errorbox("Can not track, is a directory!>");
                echo "<br/><div align='center'><a href='javascript:history.back(1)'>Back</a></div>";
            } elseif($status == 'added') {
                echo "<div class='pageheadertext'>File added to tracker</div>";
                echo "Show File here";

                echo "<br/><div align='center'><a href='javascript:history.back(1)'>Back</a></div>";
            }
        } else {
            template_headtag("Add New File");
            template_header();
            template_left();
            echo "<div class='pageheadertext'>Add New File</div><br />";
            echo "<p>Please add file path and file name separately. For example, adding a file at your domain, http://www.example.com<b>/folder</b>/<i>test.txt</i>.<br /><br />
            Enter File path as '<b>folder/</b>'. Enter File name as '<i>test.txt</i>'<br />
            
            </p>";
            echo "
            	<br />
            	<form method='post' action='addfile.php'>
                <table>
                <tr>
                <td><img src='images/add/filepath.png' />&nbsp;&nbsp;&nbsp;File Path : </td>
                <td><input type='text' name='filepath' size='100' /></td>
                </tr>
                <tr>
                <td><img src='images/add/fileadd.png' />&nbsp;&nbsp;&nbsp;File Name : </td>
                <td><input type='text' name='filename' size='100'/></td>
                </tr>
                </table>
                <br />
                <input type='submit' name='Submit' value='Add File'/>
                </form>
                ";
             
            template_footer();
             
        }
    }
?>
